package com.sims.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.sims.dao.IStnUserDao;
import com.sims.eo.StnUser;
import com.sims.util.HibernateSessionFactory;

public class StnUserDaoImp implements IStnUserDao {


	public StnUser getUserbyAccount(String userAccount) {
		// 从数据库获取对象
		StnUser stnUser = null;
		String sql = "from StnUser u where u.userAccount=?";
		Query query = HibernateSessionFactory.getSession().createQuery(sql);
		query.setParameter(0, userAccount);
		List users = query.list();
		if (users.size() != 0) {
			stnUser = (StnUser) users.get(0);
		}
		HibernateSessionFactory.closeSession();
		return stnUser;
	}


	public List<StnUser> getAllStnUser() {
		String sql = "from StnUser";
		Query query = HibernateSessionFactory.getSession().createQuery(sql);
		List<StnUser> list = query.list();
		HibernateSessionFactory.closeSession();
		return list;
	}


	public boolean deletStnUserById(Long id) {
		boolean bflag = false;
		// 打开Session
		Session session = HibernateSessionFactory.getSession();
		// 开始一个事务
		Transaction trans = session.beginTransaction();
		try {
			// 查询删除对象
			StnUser user = (StnUser) session.get(StnUser.class, id);
			// 持久化操作
			session.delete(user);
			// 提交事务
			trans.commit();
			bflag = true;
		} catch (Exception e) {
			System.out.println("error:" + e.getMessage());
			e.printStackTrace();
			// 回滚事务
			trans.rollback();
		} finally {
			// 关闭Session
			HibernateSessionFactory.closeSession();
		}
		return bflag;
	}


	public StnUser getStnUserById(Long id) {
		// 打开Session
		Session session = HibernateSessionFactory.getSession();
		// 查询删除对象
		StnUser user = (StnUser) session.get(StnUser.class, id);
		return user;
	}


	public boolean updateStnUser(StnUser user) {
		boolean bflag = false;
		// 打开Session
		Session session = HibernateSessionFactory.getSession();
		// 开始一个事务
		Transaction trans = session.beginTransaction();
		try {
			// 持久化操作
			session.update(user);
			// 提交事务
			trans.commit();
			bflag = true;
		} catch (Exception e) {
			System.out.println("error:" + e.getMessage());
			e.printStackTrace();
			// 回滚事务
			trans.rollback();
		} finally {
			// 关闭Session
			HibernateSessionFactory.closeSession();
		}
		return bflag;
	}


	public Long createStnUser(StnUser user) {
		Long id = 0L;
		// 打开Session
		Session session = HibernateSessionFactory.getSession();
		// 开始一个事务
		Transaction trans = session.beginTransaction();
		try {
			// 持久化操作
			session.save(user);
			// 提交事务
			trans.commit();
			id = user.getId();
		} catch (Exception e) {
			System.out.println("error:" + e.getMessage());
			e.printStackTrace();
			// 回滚事务
			trans.rollback();
		} finally {
			// 关闭Session
			HibernateSessionFactory.closeSession();
		}
		return id;

	}


	public List<StnUser> getAllStnUser(int pageNo, int pageSize) {
		String sql = "from StnUser";
		Query query = HibernateSessionFactory.getSession().createQuery(sql);
		query.setFirstResult((pageNo - 1) * pageSize);
		query.setMaxResults(pageSize);
		List<StnUser> list = query.list();
		HibernateSessionFactory.closeSession();
		return list;
	}


	public long getRowCount() {
		String hql = "select count(id) from StnUser";
		long count = 0;
		Query query = HibernateSessionFactory.getSession().createQuery(hql);
		count = (Long) query.uniqueResult();
		HibernateSessionFactory.closeSession();
		return count;
	}
}
